package com.jd.myaaaccount;

import com.jd.myaaaccount.model.Common;
import com.jd.myaaaccount.model.UserModel;
import com.jd.myaaaccount.util.AlertDialogHelper;
import com.jd.myaaaccount.util.ErrorCode;
import com.jd.myaaaccount.util.OperateCode;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MePage extends BasePage {
	private Button btnAbout;
	private Button btnLogout;
	private Button btnMyHome;
	private TextView tvName;
	private TextView tvNumber;

	private UserModel um;

	Handler handler;

	@Override
	public void iniPage(Activity context) {
		super.iniPage(context);

		initControl();

		um = new UserModel(context);

		handler = new Handler() {
			@Override
			public void handleMessage(android.os.Message msg) {
				pd.cancel();
				switch (msg.what) {
				case OperateCode.LOGOUT:
					if (msg.arg1 == ErrorCode.SUCCESS) {
						Intent intent = new Intent();
						intent.setClass(MePage.this.context,
								LoginActivity.class);
						MePage.this.context.startActivity(intent);
						MePage.this.context.finish();
					} else {
						Common.showErrorInfo(MePage.this.context, msg.arg1,
								"退出失败", 0);
					}
					break;

				default:
					break;
				}
			};
		};

		initMeInfo();

		btnLogout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				pd = AlertDialogHelper.showWaitProgressDialog(
						MePage.this.context, "正在退出…");
				new Thread(new Runnable() {

					@Override
					public void run() {
						// TODO Auto-generated method stub

						um.logout(handler);
					}
				}).start();
			}
		});

		btnMyHome.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent();
				intent.setClass(MePage.this.context, FamilyActivity.class);
				MePage.this.context.startActivity(intent);
			}
		});

		btnAbout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent();
				intent.setClass(MePage.this.context, AboutActivity.class);
				MePage.this.context.startActivity(intent);
			}
		});
	}

	private void initControl() {
		tvName = (TextView) context.findViewById(R.id.tvName);
		tvNumber = (TextView) context.findViewById(R.id.tvNumber);
		btnLogout = (Button) context.findViewById(R.id.btnLogout);
		btnMyHome = (Button) context.findViewById(R.id.btnMyHome);
		btnAbout = (Button) context.findViewById(R.id.btnAbout);
	}

	private void initMeInfo() {
		tvName.setText(Common.currentUser.getName());
		tvNumber.setText(String.valueOf(Common.currentUser.getNumber()));
	}
}
